package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.Channel;
import com.rockcent.mall.lib.domain.enumclass.ChannelStatus;
import com.rockcent.mall.lib.dto.ChannelDto;
import com.rockcent.mall.lib.dto.MerchantCouponDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by oyhk on 14-6-22.
 */
@Repository
public class ChannelRepositoryImpl implements ChannelRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(ChannelRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public ChannelDto queryByAppFlagAndIsDeleteAndChannelStatus(Whether yes, Whether no, ChannelStatus using) {
        SQLObjectDoing<ChannelDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from channel WHERE is_delete='NO' and app_flag='YES' and channel_status='USING' limit 1");
            fields.append("*");
        };
        return doing.go(ChannelDto.class, jdbcTemplate, log, objectMapper);
    }
}
